<?php

/**
 * home actions.
 *
 * @package    loquiero
 * @subpackage home
 * @author     loquiero
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class homeActions extends sfApplyActionsLibrary//sfActions
{


  public function executeUnderconst(sfWebRequest $request)
  {
       $this->setLayout('layout_blank');
  }
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
      
      $this->redirectIf($this->getUser()->isAuthenticated(), '@dashboard');
      $this->setLayout('layout_index');
      if($request->hasParameter('language'))
      {
        $this->getUser()->setCulture($request->getParameter('language'));
      }
      else if($this->getUser()->hasAttribute('culture'))
      {
        $this->getUser()->setCulture($request->getPreferredCulture(array('en','es')));
      }
      
   }

  public function executeRegisterlq(sfWebRequest $request)
  {
      $this->redirectIf($this->getUser()->isAuthenticated(), '@dashboard');
      $this->setLayout('layout_register');
      // we're getting default or customized applyForm for the task
      if( !( ($this->formRegister = $this->newForm('applyForm')) instanceof sfGuardUserProfileForm))
      {
          // if the form isn't instance of sfApplyApplyForm, we don't accept it
          throw new InvalidArgumentException('The custom apply form should be instance of sfApplyApplyForm');
      }

      if( $request->isMethod('post') )
      {
          //gathering form request in one array
          $formValues = $request->getParameter($this->formRegister->getName() );
          if(sfConfig::get('app_recaptcha_enabled') )
          {
            $captcha = array(
              'recaptcha_challenge_field' => $request->getParameter('recaptcha_challenge_field'),
              'recaptcha_response_field'  => $request->getParameter('recaptcha_response_field'),
            );
            //Adding captcha to form array
            $formValues = array_merge( $formValues, array('captcha' => $captcha));
          }
        //binding request form parameters with form
          $this->formRegister->bind($formValues);
          if ($this->formRegister->isValid())
          {

            $guid = "n" . self::createGuid();
            $this->formRegister->getObject()->setValidate( $guid );
            $date = new DateTime();
            $this->formRegister->getObject()->setValidateAt( $date->format( 'Y-m-d H:i:s' ) );
            $this->formRegister->save();
            $confirmation = sfConfig::get( 'app_sfForkedApply_confirmation' );
            if( $confirmation['apply'] )
            {
              try
              {
                //Extracting object and sending creating verification mail
                $profile = $this->formRegister->getObject();
                //$this->sendVerificationMail($profile);
                return 'After';
              }
              catch (Exception $e)
              {
                //Cleaning after possible exception thrown in ::sendVerificationMail() method
                $profile = $this->formRegister->getObject();
                $user = $profile->getUser();
                $user->delete();
                //We rethrow exception for the dev environment. This catch
                //catches other than mailer exception, i18n as well. So developer
                //now knows what he's up to.
                if( sfContext::getInstance()->getConfiguration()->getEnvironment() === 'dev' )
                {
                  throw $e;
                }
                return 'MailerError';
              }
            }
            else
            {
              $this->activateUser( $this->formRegister->getObject()->getUser() );
              return 'After';
            }
          }
          else
          {           
               return 'Error';
          }
        }
   }

   public function executeLoginlq(sfWebRequest $request)
   {
       $this->setLayout('layout_login');
       if($request->hasParameter('language'))
       {
         $this->getUser()->setCulture($request->getParameter('language'));
       }
       else if($this->getUser()->hasAttribute('culture'))
       {
         $this->getUser()->setCulture($request->getPreferredCulture(array('en','es')));
       }

       // codigo traido del plugin
        $user = $this->getUser();
        if ($user->isAuthenticated())
        {
          return $this->redirect('@home');
        }

        $class = sfConfig::get('app_sf_guard_plugin_signin_form', 'sfGuardFormSignin');
        $this->formLogin = new $class();

        if ($request->isMethod('post'))
        {
          $this->formLogin->bind($request->getParameter('signin'));
          if ($this->formLogin->isValid())
          {
            $values = $this->formLogin->getValues();
            $this->getUser()->signin($values['user'], array_key_exists('remember', $values) ? $values['remember'] : false);

            // always redirect to a URL set in app.yml
            // or to the referer
            // or to the homepage
            $signinUrl = sfConfig::get('app_sf_guard_plugin_success_signin_url', $user->getReferer($request->getReferer()));

            return $this->redirect('' != $signinUrl ? $signinUrl : '@home');
          }
        }
        else
        {
          if ($request->isXmlHttpRequest())
          {
            $this->getResponse()->setHeaderOnly(true);
            $this->getResponse()->setStatusCode(401);

            return sfView::NONE;
          }

          // if we have been forwarded, then the referer is the current URL
          // if not, this is the referer of the current request
          $user->setReferer($this->getContext()->getActionStack()->getSize() > 1 ? $request->getUri() : $request->getReferer());

          $module = sfConfig::get('sf_login_module');
          if ($this->getModuleName() != $module)
          {
            return $this->redirect($module.'/'.sfConfig::get('sf_login_action'));
          }

          $this->getResponse()->setStatusCode(401);
        }

   }
 /*
  


  public function executeApplyaa(sfRequest $request)
  {
   if( !( ($this->formRegister = $this->newForm( 'applyForm' ) ) instanceof sfGuardUserProfileForm) )
    {
      // if the form isn't instance of sfApplyApplyForm, we don't accept it
      throw new InvalidArgumentException(
          'The custom apply form should be instance of sfApplyApplyForm' );
    }

    //Code below is used when user is sending his application!
    if( $request->isMethod('post') )
    {
      //gathering form request in one array
      $formValues = $request->getParameter($this->formRegister->getName() );
      if(sfConfig::get('app_recaptcha_enabled') )
      {
        $captcha = array(
          'recaptcha_challenge_field' => $request->getParameter('recaptcha_challenge_field'),
          'recaptcha_response_field'  => $request->getParameter('recaptcha_response_field'),
        );
        //Adding captcha to form array
        $formValues = array_merge( $formValues, array('captcha' => $captcha));
      }
        //binding request form parameters with form
      $this->formRegister->bind($formValues);
      if ($this->formRegister->isValid())
      {

        $guid = "n" . self::createGuid();
        $this->formRegister->getObject()->setValidate( $guid );
        $date = new DateTime();
        $this->formRegister->getObject()->setValidateAt( $date->format( 'Y-m-d H:i:s' ) );
        $this->formRegister->save();
        $confirmation = sfConfig::get( 'app_sfForkedApply_confirmation' );
        if( $confirmation['apply'] )
        {
          try
          {
            //Extracting object and sending creating verification mail
            $profile = $this->formRegister->getObject();
            $this->sendVerificationMail($profile);
            return 'After';
          }
          catch (Exception $e)
          {
            //Cleaning after possible exception thrown in ::sendVerificationMail() method
            $profile = $this->formRegister->getObject();
            $user = $profile->getUser();
            $user->delete();
            //We rethrow exception for the dev environment. This catch
            //catches other than mailer exception, i18n as well. So developer
            //now knows what he's up to.
            if( sfContext::getInstance()->getConfiguration()->getEnvironment() === 'dev' )
            {
              throw $e;
            }
            return 'MailerError';
          }
        }
        else
        {
          $this->activateUser( $this->formRegister->getObject()->getUser() );
          return $this->redirect( '@home' );
          //return 'After';
        }
      }
      
    }
     
  }*/


  public function preExecute()
  {
      
    /*if (sfFacebook::isInsideFacebook())
    {
        $this->setLayout('layout_fbml');
    }
    else
    {
        $this->setLayout('layout_connect');
    }*/
  }
   

}
